package com.huarui.companypackage.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huarui.company.model.CompanyMarket;
import com.huarui.companypackage.dao.ICompanyMarketDao;
import com.huarui.companypackage.util.CompanyPackageConstant;
import com.huarui.market.dao.IMarketDao;
import com.huarui.market.model.Market;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
/**
 * 大宗贸易商，汽车配件厂购买套餐,companyMarket保存规则
 * @author Administrator
 *
 */
public class CompanyMarket4MerchantAndAutoMobileService implements CompanyMarketByCompanyTypeService{
	
	private IMarketDao marketDao;
	private ICompanyMarketDao companyMarketDao;
	
	@Override
	public Map<String,List<CompanyMarket>> getCompanyMarket4CompanyByType(List<String> marketIds,int duration,Integer mainMarketId){
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		Integer companyId = sessionMessage.getCompanyId();
		List<CompanyMarket> companyAddedMarkets = new ArrayList<CompanyMarket>();
		List<CompanyMarket> companyUpdateMarkets = new ArrayList<CompanyMarket>();
		List<CompanyMarket> companyMarkets = companyMarketDao.getAllCompanyMarketByCompanyId(companyId);
		if(companyMarkets.isEmpty()){
			//首次购买套餐
			List<Market> secLevelmarkets = marketDao.getMarketByMarketType(Market.MARKET_TYPE_2_LEVEL);//八个二级市场
			Market firLevelMarket = marketDao.getMarketByMarketType(Market.MARKET_TYPE_1_LEVEL).get(0);//一级市场
			for(Market market : secLevelmarkets){
				//增加搜索市场
				CompanyMarket searchMarket = new CompanyMarket();
				searchMarket.setCompanyId(companyId);
				searchMarket.setDuration(duration);
				searchMarket.setMarketId(market.getId());
				searchMarket.setSearchable(CompanyMarket.SEARCHABLE);
				searchMarket.setBefore(false);
				companyAddedMarkets.add(searchMarket);
			}
			//增加一级搜索市场
			CompanyMarket firLevelSearchMarket = new CompanyMarket();
			firLevelSearchMarket.setCompanyId(companyId);
			firLevelSearchMarket.setDuration(duration);
			firLevelSearchMarket.setMarketId(firLevelMarket.getId());
			firLevelSearchMarket.setSearchable(CompanyMarket.SEARCHABLE);
			firLevelSearchMarket.setBefore(false);
			companyAddedMarkets.add(firLevelSearchMarket);
			//增加一级发布市场
			CompanyMarket firLevelPublishMarket = new CompanyMarket();
			firLevelPublishMarket.setCompanyId(companyId);
			firLevelPublishMarket.setDuration(duration);
			firLevelPublishMarket.setMarketId(firLevelMarket.getId());
			firLevelPublishMarket.setPublishable(CompanyMarket.PUBLISHABLE);
			firLevelPublishMarket.setBefore(false);
			companyAddedMarkets.add(firLevelPublishMarket);
		}else{
			//更新大宗贸易商，汽车配件厂的companyMarket
			//当前购买时间(数据库时间)
			Date curDay = companyMarketDao.getCurDay();
			//当前使用套餐到期时间
			Date overDue = companyMarkets.get(0).getGmtEnd();
			//判断下一个套餐购买时前一个套餐是否已到期
			int daysBetween = curDay.compareTo(overDue);
			for(CompanyMarket companyMarket : companyMarkets){
				if(daysBetween <= 0){
					//套餐未到期前购买套餐，更新该companyMarket的gmtEnd,gmtModify
					companyMarket.setDuration(duration);
					companyMarket.setGmtEnd(companyMarket.getGmtEnd());
					companyMarket.setBefore(true);								
				}else if(daysBetween > 0){
					//套餐已过期再购买套餐,更新gmtBegin,gmtEnd,gmtModify
					companyMarket.setDuration(duration);
					companyMarket.setBefore(false);																
				}
				companyUpdateMarkets.add(companyMarket);
			}			
		}				
		Map<String,List<CompanyMarket>> map = new HashMap<String, List<CompanyMarket>>();
		map.put(CompanyPackageConstant.COMPANYADDEDMARKETS.getType(), companyAddedMarkets);
		map.put(CompanyPackageConstant.COMPANYUPDATEMARKETS.getType(),companyUpdateMarkets);
		return map;
	}

	public void setMarketDao(IMarketDao marketDao) {
		this.marketDao = marketDao;
	}

	public void setCompanyMarketDao(ICompanyMarketDao companyMarketDao) {
		this.companyMarketDao = companyMarketDao;
	}

}
